//HY11 23N1_2
#include "PlatformProperty.h"

MAPPING_VALUE(sdb::SwtVertSts1, SeatCushionTiltMode, SeatCushionTiltMode::SEAT_CUSHION_TILT_OFF)
MAPPING_ITEM(sdb::SwtVertSts1::Idle, SeatCushionTiltMode::SEAT_CUSHION_TILT_OFF)
MAPPING_ITEM(sdb::SwtVertSts1::Up, SeatCushionTiltMode::SEAT_CUSHION_TILT_UP)
MAPPING_ITEM(sdb::SwtVertSts1::Dwn, SeatCushionTiltMode::SEAT_CUSHION_TILT_DOWN)
MAPPING_END

sdb::Boolean m_LastDrvrSeatExtAdjAllowd_SeatExt3 = sdb::Boolean::FALSE;
std::map<int32_t, int32_t> m_SeatAdjustmentVFC;

void SeatAdjustmentChangeVFC_IdleToOther(SignalTX iSignal, int32_t iValue) {
    int32_t ikey = static_cast<int32_t>(iSignal);
    std::map<int32_t, int32_t>::iterator it = m_SeatAdjustmentVFC.find(ikey);
     int32_t idle = static_cast<int32_t>(sdb::SwtVertSts1::Idle);
    if (it == m_SeatAdjustmentVFC.end()) {
        m_SeatAdjustmentVFC[ikey] = idle;
    }
    VHALOG.d("SeatAdjustmentChangeVFC_IdleToOther-iValue=%d,last=%d,Signal=%d,key=%d", iValue, m_SeatAdjustmentVFC[ikey],
          iSignal, ikey);
    if (idle == m_SeatAdjustmentVFC[ikey] &&
            idle != iValue && !SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd)) {
        SIGNAL.setCB_VFC_SeatComfortFunctions(sdb::OnOff1::On);
    }
    m_SeatAdjustmentVFC[ikey] = iValue;
}

// [SW_FC_004_045]第一排座垫折叠调节1stRowSeatCushionTiltAdjustment
SYSTEM_READ_WRITE_PROPERTY(SEAT_TILT_MOVE, SeatCushionTiltMode) {
    //第一排主驾座椅折叠
    builder.area(VehicleAreaSeat::ROW_1_LEFT)
            .initValue([]() {
                return SeatCushionTiltMode::SEAT_CUSHION_TILT_OFF;
            })
            .withStatusBySignals([]([[maybe_unused]] const PropertyStatus &lastStatus) {
                sdb::Boolean DrvrSeatExtAdjAllowd = SIGNAL.getDrvrSeatExtAdjAllowd();
                sdb::Timeout DrvrSeatExtAdj_AllowdTimeout = SIGNAL.getDrvrSeatExtAdjAllowdTimeout();
                sdb::GearLvrIndcn2 GearLvrIndcn2 = SIGNAL.getGearLvrIndcn();

                if (DrvrSeatExtAdj_AllowdTimeout == sdb::Timeout::Timeout) {
                    if (SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModActv, sdb::UsgModSts1::UsgModDrvg)) {
                        DrvrSeatExtAdjAllowd = sdb::Boolean::FALSE;
                    } else {
                        DrvrSeatExtAdjAllowd = m_LastDrvrSeatExtAdjAllowd_SeatExt3;
                    }
                }
                if(GearLvrIndcn2 != sdb::GearLvrIndcn2::ParkIndcn){
                    DrvrSeatExtAdjAllowd = sdb::Boolean::FALSE;
                }
                
                VHALOG.d("SEAT_TILT_MOVE DRVR status Change carmod[%d], usgmod[%d], cc468[%d], cc1173[%d], DrvrSeatExtAdjAllowd[%d], DrvrSeatExtAdj_AllowdTimeout[%d],GearLvrIndcn2[%d]", SIGNAL.getVehModMngtGlbSafe1CarModSts1(),
                    SIGNAL.getVehModMngtGlbSafe1UsgModSts(), SIGNAL.getcarconfig468(), SIGNAL.getcarconfig1173() ,DrvrSeatExtAdjAllowd, DrvrSeatExtAdj_AllowdTimeout,GearLvrIndcn2);
                m_LastDrvrSeatExtAdjAllowd_SeatExt3 = DrvrSeatExtAdjAllowd;
                if (SIGNAL.carConfigIs(SignalRX::carconfig468, sdb::CarConfig468::_02, sdb::CarConfig468::_03)
                    && SIGNAL.carConfigIs(SignalRX::carconfig1173, sdb::CarConfig1173::_02)) {
                    if (SIGNAL.carModeIs(sdb::CarModSts1::CarModCrash, sdb::CarModSts1::CarModDyno,
                                        sdb::CarModSts1::CarModNorm) &&
                        SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModInActv, sdb::UsgModSts1::UsgModCnvinc,
                                        sdb::UsgModSts1::UsgModActv, sdb::UsgModSts1::UsgModDrvg) &&
                        (DrvrSeatExtAdjAllowd == sdb::Boolean::TRUE)) {
                        VHALOG.d("SEAT_TILT_MOVE DRVR Active");
                        return PropertyStatus::Active;
                    }
                    VHALOG.d("SEAT_TILT_MOVE DRVR NotActive");
                    return PropertyStatus::NotActive;
                }
                VHALOG.d("SEAT_TILT_MOVE DRVR NotAvailable");
                return PropertyStatus::NotAvailable;
            },
            SignalRX::carconfig468,
            SignalRX::carconfig1173,
            SignalRX::DrvrSeatExtAdjAllowd,
            SignalRX::DrvrSeatExtAdjAllowdTimeout,
            SignalRX::VehModMngtGlbSafe1UsgModSts,
            SignalRX::VehModMngtGlbSafe1CarModSts1,
            SignalRX::GearLvrIndcn)
            .withValueBySignals([]([[maybe_unused]] const SeatCushionTiltMode &last) {
                //座椅折叠状态
                auto heiFrntSts = SIGNAL.getDrvrSeatSwtStsDrvrSeatSwtHeiFrntSts();
                //座椅坐垫实时位置
                auto frntHeiPerc = static_cast<int32_t>(SIGNAL.getDrvrSeatPosPercSeatPosFrntHeiPerc());
                //信号质量
                bool qfIsReaday = SIGNAL.getDrvrSeatPosPercSeatPosFrntHeiQF() == sdb::Qf1::DataCalcdWithDevDefd;
                //物理按键压下状态
                auto drvrSeatBtnPsd = SIGNAL.getDrvrSeatBtnPsd();

                VHALOG.d("SEAT_TILT_MOVE DRVR withValueBySignals heiFrntSts[%d],frntHeiPerc[%d],qfIsReaday[%d],drvrSeatBtnPsd[%d]",toInt(heiFrntSts),frntHeiPerc,static_cast<int32_t>(qfIsReaday),toInt(drvrSeatBtnPsd));
                if(drvrSeatBtnPsd == sdb::Boolean::TRUE||// 硬按键按下时需要按idle处理
                    (qfIsReaday && (frntHeiPerc == 100 && heiFrntSts == sdb::SwtVertSts1::Up)) || // 无法再向上
                    (qfIsReaday && (frntHeiPerc == 0 && heiFrntSts == sdb::SwtVertSts1::Dwn))){// 无法再向下
                    VHALOG.d("SEAT_TILT_MOVE DRVR trigger to idle");
                    SIGNAL.setSeatCushTiltAdjmtRowFirstDrvr(sdb::SwtVertSts1::Idle);
                    return SeatCushionTiltMode::SEAT_CUSHION_TILT_OFF;
                }
                return mappingValue(heiFrntSts, SeatCushionTiltMode::SEAT_CUSHION_TILT_OFF);
            }, 
            SignalRX::DrvrSeatSwtStsDrvrSeatSwtHeiFrntSts,
            SignalRX::DrvrSeatBtnPsd,
            SignalRX::DrvrSeatPosPercSeatPosFrntHeiPerc,
            SignalRX::DrvrSeatPosPercSeatPosFrntHeiQF)
            .onSetProperty([](const SeatCushionTiltMode value) {
                if (value !=  SeatCushionTiltMode::SEAT_CUSHION_TILT_OFF
                            && value !=  SeatCushionTiltMode::SEAT_CUSHION_TILT_DOWN
                            && value !=  SeatCushionTiltMode::SEAT_CUSHION_TILT_UP) {
                    VHALOG.d("SEAT_TILT_MOVE DRVR: param invaild");
                    return;
                }
                VHALOG.d("SEAT_TILT_MOVE DRVR: setValue[%d]", value);
                sdb::SwtVertSts1 TempSeatCushTiltAdjmtRowFirstDrvr = sdb::SwtVertSts1::Idle;
                if (value == SeatCushionTiltMode::SEAT_CUSHION_TILT_OFF) {
                    VHALOG.d("SEAT_TILT_MOVE DRVR  Idle");
                    TempSeatCushTiltAdjmtRowFirstDrvr = sdb::SwtVertSts1::Idle;
                } else if (value == SeatCushionTiltMode::SEAT_CUSHION_TILT_UP) {
                    VHALOG.d("SEAT_TILT_MOVE DRVR Up");
                    TempSeatCushTiltAdjmtRowFirstDrvr = sdb::SwtVertSts1::Up;
                } else {
                    VHALOG.d("SEAT_TILT_MOVE DRVR Dwn");
                    TempSeatCushTiltAdjmtRowFirstDrvr = sdb::SwtVertSts1::Dwn;
                }
                SeatAdjustmentChangeVFC_IdleToOther(SignalTX::SeatCushTiltAdjmtRowFirstDrvr,
                                                     static_cast<int32_t>(TempSeatCushTiltAdjmtRowFirstDrvr));
                SIGNAL.setSeatCushTiltAdjmtRowFirstDrvr(TempSeatCushTiltAdjmtRowFirstDrvr);
            })
            //第一排副驾座椅折叠
            .area(VehicleAreaSeat::ROW_1_RIGHT)
            .initValue([]() {
                return SeatCushionTiltMode::SEAT_CUSHION_TILT_OFF;
            })
            .withStatusBySignals([]([[maybe_unused]] const PropertyStatus &lastStatus) {
                VHALOG.d("SEAT_TILT_MOVE PASS status Change carmod[%d], usgmod[%d], cc468[%d], cc1174[%d]", SIGNAL.getVehModMngtGlbSafe1CarModSts1(),
                    SIGNAL.getVehModMngtGlbSafe1UsgModSts(), SIGNAL.getcarconfig468(), SIGNAL.getcarconfig1174());
                if (SIGNAL.carConfigIs(SignalRX::carconfig468, sdb::CarConfig468::_02)
                    && SIGNAL.carConfigIs(SignalRX::carconfig1174, sdb::CarConfig1174::_02)) {
                    if (SIGNAL.carModeIs(sdb::CarModSts1::CarModCrash, sdb::CarModSts1::CarModDyno,
                                        sdb::CarModSts1::CarModNorm) &&
                        SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModInActv, sdb::UsgModSts1::UsgModCnvinc,
                                        sdb::UsgModSts1::UsgModActv, sdb::UsgModSts1::UsgModDrvg)) {
                        VHALOG.d("SEAT_TILT_MOVE PASS Active");
                        return PropertyStatus::Active;
                    }
                    VHALOG.d("SEAT_TILT_MOVE PASS NotActive");
                    return PropertyStatus::NotActive;
                }
                VHALOG.d("SEAT_TILT_MOVE PASS NotAvailable");
                return PropertyStatus::NotAvailable;
            },
            SignalRX::carconfig468,
            SignalRX::carconfig1174,
            SignalRX::VehModMngtGlbSafe1UsgModSts,
            SignalRX::VehModMngtGlbSafe1CarModSts1)
            .withValueBySignals([]([[maybe_unused]] const SeatCushionTiltMode &last) {
                //座椅折叠状态
                auto heiFrntSts = SIGNAL.getPassSeatSwtSts2PassSeatSwtHeiFrntSts();
                //座椅坐垫实时位置
                auto frntHeiPerc = static_cast<int32_t>(SIGNAL.getPassSeatPosPercSeatPosFrntHeiPerc());
                //信号质量
                bool qfIsReaday = SIGNAL.getPassSeatPosPercSeatPosFrntHeiQF() == sdb::Qf1::DataCalcdWithDevDefd;
                //物理按键压下状态
                auto passSeatBtnPsd = SIGNAL.getPassSeatBtnPsd();

                VHALOG.d("SEAT_TILT_MOVE PASS withValueBySignals heiFrntSts[%d],frntHeiPerc[%d],qfIsReaday[%d],passSeatBtnPsd[%d]",toInt(heiFrntSts),frntHeiPerc,static_cast<int32_t>(qfIsReaday),toInt(passSeatBtnPsd));

                if(passSeatBtnPsd == sdb::Boolean::TRUE||// 硬按键按下时需要按idle处理
                    (qfIsReaday && (frntHeiPerc == 100 && heiFrntSts == sdb::SwtVertSts1::Up)) || // 无法再向上
                    (qfIsReaday && (frntHeiPerc == 0 && heiFrntSts == sdb::SwtVertSts1::Dwn))){// 无法再向下
                    VHALOG.d("SEAT_TILT_MOVE PASS trigger to idle");
                    SIGNAL.setSeatCushTiltAdjmtRowFirstPass(sdb::SwtVertSts1::Idle);
                    return SeatCushionTiltMode::SEAT_CUSHION_TILT_OFF;
                }
                return mappingValue(heiFrntSts, SeatCushionTiltMode::SEAT_CUSHION_TILT_OFF);
            }, 
            SignalRX::PassSeatSwtSts2PassSeatSwtHeiFrntSts,
            SignalRX::PassSeatBtnPsd,
            SignalRX::PassSeatPosPercSeatPosFrntHeiQF,
            SignalRX::PassSeatPosPercSeatPosFrntHeiPerc)
            .onSetProperty([](const SeatCushionTiltMode value) {
                if (value !=  SeatCushionTiltMode::SEAT_CUSHION_TILT_OFF
                            && value !=  SeatCushionTiltMode::SEAT_CUSHION_TILT_DOWN
                            && value !=  SeatCushionTiltMode::SEAT_CUSHION_TILT_UP) {
                    VHALOG.d("SEAT_TILT_MOVE PASS: param invaild");
                    return;
                }
                VHALOG.d("SEAT_TILT_MOVE PASS : setValue[%d]", value);
                sdb::SwtVertSts1 TempSeatCushTiltAdjmtRowFirstPass = sdb::SwtVertSts1::Idle;
                if (value == SeatCushionTiltMode::SEAT_CUSHION_TILT_OFF) {
                    VHALOG.d("SEAT_TILT_MOVE PASS Idle");
                    TempSeatCushTiltAdjmtRowFirstPass = sdb::SwtVertSts1::Idle;
                } else if (value == SeatCushionTiltMode::SEAT_CUSHION_TILT_UP) {
                    VHALOG.d("SEAT_TILT_MOVE PASS Up");
                    TempSeatCushTiltAdjmtRowFirstPass = sdb::SwtVertSts1::Up;
                } else {
                    VHALOG.d("SEAT_TILT_MOVE PASS Dwn");
                    TempSeatCushTiltAdjmtRowFirstPass = sdb::SwtVertSts1::Dwn;
                }
                SeatAdjustmentChangeVFC_IdleToOther(SignalTX::SeatCushTiltAdjmtRowFirstPass,
                                                     static_cast<int32_t>(TempSeatCushTiltAdjmtRowFirstPass));
                SIGNAL.setSeatCushTiltAdjmtRowFirstPass(TempSeatCushTiltAdjmtRowFirstPass);
            });
}
